package com.prs.crm.action.warning;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;

import com.prs.crm.domain.warning.AutoCode;
import com.prs.crm.service.warning.AutoCodeService;

/**
 * @author JohnLan
 * @time：2012-11-29
 * @version 1.0 类说明
 */

public class AutoCodeAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private AutoCodeService autoCodeService;

	/***
	 * 根据类型进行编码的保存
	 * 
	 * @param type
	 */
	public String getCode(String type) {
    AutoCode alist=this.getAutoCodeService().getTaskRemind(type);
    Date dat=new Date();
    SimpleDateFormat adf=new SimpleDateFormat("yyyyMMdd");
    String date=adf.format(dat);
    if(alist==null)
    {//初始的时候调用
    AutoCode  ac=new AutoCode();
    ac.setCodeType(type);
    ac.setCodeValue(type+date+"0000001");
    ac.setAddValue("0000001");
    this.getAutoCodeService().save(ac);	
    return type+date+"0000001";
    }else{
    AutoCode  ac=alist;
    ac.setCodeValue(type+date+addCodes(ac.getAddValue()));
    ac.setAddValue(addCodes(ac.getAddValue()));
    this.getAutoCodeService().submit(ac);	
    return type+date+addCodes(ac.getAddValue());
    }
	}
	/**
	 * 自增计算
	 * @param num
	 * @return  
	 * 目前支持流动的6为计算
	 */
    public String addCodes(String num)
    {
    	if(num!=null && !"".equals(num))
    	{
	        int currentNum=Integer.parseInt(num);
	        int nowNum=currentNum+1;       
	        return adzero(nowNum)+nowNum;
    	}
    	return "000000";
    }
	/**
	 * 需要零的个数
	 * @param sero
	 * @return
	 */
	public String adzero(int sero)
	{
		int seroA=7;
		String curlength=sero+"";
		int j=curlength.trim().length();
		String zy="";
		for(int i=0;i<seroA-j; i++)
		{
			zy=zy+"0";
		}		
		return zy.trim();	
	}
	
	
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public AutoCodeService getAutoCodeService() {
		return autoCodeService;
	}

	public void setAutoCodeService(AutoCodeService autoCodeService) {
		this.autoCodeService = autoCodeService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
public static void main(String args[])
{
	System.out.println(new AutoCodeAction().addCodes("0000001"));
}
}
